//
//  ShopBottomView.m
//  RentTable
//
//  Created by xchuan xchuan on 2024/12/1.
//

#import "ShopBottomView.h"

@interface ShopBottomView()

@property (nonatomic, strong) UIButton *shopSelectButton;
@property (nonatomic, strong) UILabel *hotPriceLabel;
@property (nonatomic, strong) UILabel *hotPriceDayLabel;
@property (nonatomic, strong) UILabel *hotLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIButton *confirmButton;

@end

@implementation ShopBottomView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

- (void)updateBottomView:(NSInteger)sumPrice {
    if (sumPrice > 0) {
        [self.priceLabel setHidden:false];
        [self.hotPriceDayLabel setHidden:false];
        self.confirmButton.selected = true;
        [self.confirmButton setBackgroundColor:myRGBHex(0xFF0AA1)];
        self.priceLabel.text = @(sumPrice).stringValue;
    }else {
        [self.priceLabel setHidden:true];
        [self.hotPriceDayLabel setHidden:true];
        self.confirmButton.selected = false;
        [self.confirmButton setBackgroundColor:myRGBHex(0xCCCCCC)];
        self.shopSelectButton.selected = false;
    }
}

- (void)updateAllTitle:(NSInteger)count {
    self.hotPriceLabel.text = [NSString stringWithFormat:@"全选(%ld)",(long)count];
}

- (void)setUI {
    
    self.shopSelectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.shopSelectButton setBackgroundImage:[UIImage imageNamed:@"未勾选"] forState:UIControlStateNormal];
    [self.shopSelectButton setBackgroundImage:[UIImage imageNamed:@"勾选"] forState:UIControlStateSelected];
    [self.shopSelectButton addTarget:self action:@selector(shopSelectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.shopSelectButton];
    [self.shopSelectButton setHidden:true];
    [self.shopSelectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.leading.mas_equalTo(19);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    self.hotPriceLabel = [[UILabel alloc] init];
    self.hotPriceLabel.font = [UIFont pingFangSCMediumWithSize:14];
    self.hotPriceLabel.textColor = myRGBHex(0x333333);
    self.hotPriceLabel.text = @"全选 (2)";
    [self.hotPriceLabel setHidden:true];
    self.hotPriceLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:self.hotPriceLabel];
    [self.hotPriceLabel sizeToFit];
    [self.hotPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.shopSelectButton.mas_centerY);
        make.leading.equalTo(self.shopSelectButton.mas_trailing).offset(9);
        make.height.mas_equalTo(18);
    }];
    
    self.hotPriceDayLabel = [[UILabel alloc] init];
    self.hotPriceDayLabel.font = [UIFont pingFangSCRegularWithSize:14];
    self.hotPriceDayLabel.textColor = myRGBHex(0x333333);
    self.hotPriceDayLabel.textAlignment = NSTextAlignmentLeft;
    self.hotPriceDayLabel.text = @"合计：";
    [self.hotPriceDayLabel setHidden:true];
    [self addSubview:self.hotPriceDayLabel];
    [self.hotPriceDayLabel sizeToFit];
    [self.hotPriceDayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.shopSelectButton.mas_centerY);
        make.leading.equalTo(self.hotPriceLabel.mas_trailing).offset(52);
        make.height.mas_equalTo(18);
    }];
    
    self.priceLabel = [[UILabel alloc] init];
    self.priceLabel.font = [UIFont pingFangSCRegularWithSize:14];
    self.priceLabel.textColor = myRGBHex(0xF51D1D);
    self.priceLabel.textAlignment = NSTextAlignmentLeft;
    self.priceLabel.text = @"¥3797";
    [self.priceLabel setHidden:true];
    [self addSubview:self.priceLabel];
    [self.priceLabel sizeToFit];
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.shopSelectButton.mas_centerY);
        make.leading.equalTo(self.hotPriceDayLabel.mas_trailing).offset(1);
        make.height.mas_equalTo(18);
    }];
    
    self.confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.confirmButton setTitle:@"立即结算" forState:UIControlStateNormal];
    [self.confirmButton setTitleColor:myRGBHex(0xFFFFFF) forState:UIControlStateNormal];
    self.confirmButton.titleLabel.font = [UIFont pingFangSCMediumWithSize:15];
    [self.confirmButton setBackgroundColor:myRGBHex(0xCCCCCC)];
    [self.confirmButton addTarget:self action:@selector(confirmButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.confirmButton];
    [self.confirmButton.titleLabel sizeToFit];
    self.confirmButton.frame = CGRectMake(spDeviceWidth - 108, 16, 91, 32);
    self.confirmButton.layer.cornerRadius = 16;
    self.confirmButton.layer.masksToBounds = true;
}

- (void)shopSelectButtonClicked:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (self.shopAllSelect) {
        self.shopAllSelect(sender.selected);
    }
}

- (void)confirmButtonPressed:(UIButton *)sender {
    
    if (self.shopComplete && sender.selected) {
        self.shopComplete();
    }
    
}

@end
